﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace program1
{
    public class StudentService
    {
        private readonly StudentRepository studentRepository;

        public StudentService(string connectionstring)
        {
            this.studentRepository = new StudentRepository(connectionstring);
        }

        public List<Student> GetAllStudents()
        {
            try
            {
                return studentRepository.GetAllStudents();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return null;
            }
        }

        public List<Student> GetStudentsByClass(int classID)
        {
            try
            {
                return studentRepository.GetStudentsByClass(classID);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return null;
            }
        }

        public void AddStudent(int classID, string name, int age, string gender, string address)
        {
            try
            {
                studentRepository.InsertStudent(classID, name, age, gender, address);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void DeleteStudent(int studentID)
        {
            try
            {
                studentRepository.DeleteStudent(studentID);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void UpdateStudent(int studentID, string name, int age, string gender, string address)
        {
            try
            {
                studentRepository.UpdateStudent(studentID, name, age, gender, address);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public List<Student> GetStudents(int studentID)
        {
            // 在此编写查询学生数据的业务逻辑
            try
            {
                // 调用 studentRepository.GetStudents(studentID) 以获取学生数据
                List<Student> students = studentRepository.GetStudents(studentID);

                // 返回查询结果
                return students;
            }
            catch (Exception ex)
            {
                // 处理异常或记录错误日志
                Console.WriteLine("Error: " + ex.Message);
                return null; // 或者抛出异常，具体取决于你的需求
            }
        }
    }

}
